<?php

namespace App\Http\Requests\Admin\CollPosition;
use App\Http\Requests\Request;
use App\Models\Common\CollPosition;
use Illuminate\Support\Facades\Validator;

class PostCreateRequest extends Request{
    public function authorize(){
        return true;
    }
    public function rules(){
        //添加职位排序判断
        Validator::extend('check_position_sort', function($attribute, $value, $parameters) {
            if($value!=0){
                $collPositionSort = CollPosition::where('grade','1')->pluck('sort')->toArray();
                if(in_array($value,$collPositionSort)){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        });
        return [
            'name'=>'required',
            'cid'=>'required|numeric|max:999999|unique:coll_position,cid|cate_match',
            'sort'=>'check_position_sort'
        ];
    }
    public function messages(){
        return [
            'name.required'=>'职位大类名称必须填写',
            'cid.required'=>'职位大类编码必须填写',
            'cid.unique'=>'职位大类编码已经存在',
            'cid.numeric'=>'职位大类编码必须是一个数字',
            'cid.max'=>'职位大类编码必须是一个6数字',
            'cid.cate_match'=>'职位大类编码必须是以0000结尾的6位数数字组合',
            'sort.check_position_sort'=>'职位大类排序已经存在',
        ];
    }
}
